package spotGUI.personalProfileGUI;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import net.tomahawk.ExtensionsFilter;
import net.tomahawk.XFileDialog;
import personalProfile.PhotoManager;
import personalProfile.PhotoManager.Photo;
import personalProfile.PhotoManager.PhotoAlbum;
import utils.MaxLenghtTextDocument;

public class AddPhotosPanel extends JPanel {

	private JLabel lblCreaNuovoAlbum;
	private JButton btnCopertinaAlbum;
	private JLabel lblCopertina;
	private JLabel lblNomeAlbum;
	private JTextField textField;
	private JLabel lblDescrizioneAlbum;
	private JTextArea txtrDescrizioneAlbum;
	private JScrollPane scrollPane;
	private JLabel lblSelectedAlbum;
	private JLabel lblPendingphotos;
	private JButton btnReset;
	private JButton btnSelezionaUnaFoto;
	private JButton btnSelezionaPiFoto;
	private JButton btnAggiungi;
	private JTextArea textAreaDescrizioneFoto;
	private JScrollPane scrollPane_1;

	private PhotoAlbum currentlySelectedAlbum;

	private Font font = new Font("Agency FB", Font.BOLD, 23);
	private JButton btnAggiungiAlbum;

	private PhotoManager manager;

	private String pendingAlbumCoverPath;
	private ArrayList<Photo> pendingPhotos;

	private XFileDialog fileChooser;


	private ActionListener addAlbumListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (!textField.getText().equals("")) {
				PhotoAlbum album = manager.createPendingAlbum(textField.getText(), txtrDescrizioneAlbum.getText(),
						pendingAlbumCoverPath, new Timestamp(Calendar.getInstance().getTimeInMillis()));
				manager.addPendingAlbum(album);
				resetCreateAlbumWindow();
			}
		}
	};


	private ActionListener albumCoverListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			String fileName = fileChooser.getFile();
			String directory = fileChooser.getDirectory();
			pendingAlbumCoverPath = directory+fileName;
			lblCopertina.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.labelLoaded"));
		}
	};


	private ActionListener resetAddPhotosWindowListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			resetAddPhotosWindow();
		}
	};

	private ActionListener addPhotosListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			manager.addPendingPhoto(pendingPhotos);
			resetAddPhotosWindow();
		}
	};


	private ActionListener selectPhotoListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if (currentlySelectedAlbum == null)
				return;
			String fileName = fileChooser.getFile();
			String directory = fileChooser.getDirectory();
			if (fileName != null) {
				pendingPhotos.add(manager.createPendingPhoto(currentlySelectedAlbum.albumid, textAreaDescrizioneFoto.getText(), directory+fileName));
				lblPendingphotos.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.toAdd")+pendingPhotos.size());
			}
		}
	};

	private ActionListener selectMultiplePhotoListener = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if (currentlySelectedAlbum == null)
				return;
			String[] fileName = fileChooser.getFiles();
			String directory = fileChooser.getDirectory();
			if (fileName != null) {
				int userFeedback = JOptionPane.OK_OPTION;
				if (fileName.length > 5)
					userFeedback = JOptionPane.showConfirmDialog(null, ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.question")+fileName.length+
							ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.question2"),
							ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.question3"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
				if (userFeedback == JOptionPane.OK_OPTION) {
					for (int i = 0; i < fileName.length; i++) {
						pendingPhotos.add(manager.createPendingPhoto(currentlySelectedAlbum.albumid, textAreaDescrizioneFoto.getText(), directory+fileName[i]));
					}
					lblPendingphotos.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.toAdd")+pendingPhotos.size());
				}
			}
		}
	};

	private ActionListener modifyCoverListener = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			if (currentlySelectedAlbum == null)
				return;
			String fileName = fileChooser.getFile();
			String directory = fileChooser.getDirectory();
			if (fileName != null) {
				manager.addPendingNewAlbumCover(currentlySelectedAlbum.albumid, directory+fileName);
			}
		}
	};
	private JLabel lblAlbumReminder;
	private Box verticalBox;
	private JButton btnModificaCopertina;
	private Component verticalStrut;
	private Box horizontalBox;
	private Component horizontalStrut;


	public AddPhotosPanel(PhotoManager manager) {
		setMinimumSize(new Dimension(600, 200));

		this.manager = manager;
		pendingPhotos = new ArrayList<Photo>();

		fileChooser = new XFileDialog("Sfoglia");
		Vector<String> extensions = new Vector<String>();
		extensions.addElement("png");
		extensions.addElement("jpg");
		extensions.addElement("bmp");
		fileChooser.addFilters(new ExtensionsFilter("File immagine", extensions));

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{122, 0, 108, 31, 0, 40, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 47, 0, 31, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 2.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		lblCreaNuovoAlbum = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblCreaNuovoAlbum.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblCreaNuovoAlbum.setFont(font);
		GridBagConstraints gbc_lblCreaNuovoAlbum = new GridBagConstraints();
		gbc_lblCreaNuovoAlbum.gridwidth = 3;
		gbc_lblCreaNuovoAlbum.insets = new Insets(0, 0, 5, 5);
		gbc_lblCreaNuovoAlbum.gridx = 0;
		gbc_lblCreaNuovoAlbum.gridy = 0;
		add(lblCreaNuovoAlbum, gbc_lblCreaNuovoAlbum);

		lblNomeAlbum = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblNomeAlbum.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblNomeAlbum = new GridBagConstraints();
		gbc_lblNomeAlbum.anchor = GridBagConstraints.EAST;
		gbc_lblNomeAlbum.insets = new Insets(0, 0, 5, 5);
		gbc_lblNomeAlbum.gridx = 0;
		gbc_lblNomeAlbum.gridy = 1;
		add(lblNomeAlbum, gbc_lblNomeAlbum);

		textField = new JTextField();
		textField.setDocument(new MaxLenghtTextDocument(50));
		textField.setBackground(UIManager.getColor("Desktop.background"));
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 1;
		add(textField, gbc_textField);
		textField.setColumns(10);

		lblSelectedAlbum = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblSelectedAlbum.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblSelectedAlbum = new GridBagConstraints();
		gbc_lblSelectedAlbum.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblSelectedAlbum.gridwidth = 2;
		gbc_lblSelectedAlbum.insets = new Insets(0, 0, 5, 0);
		gbc_lblSelectedAlbum.gridx = 4;
		gbc_lblSelectedAlbum.gridy = 1;
		add(lblSelectedAlbum, gbc_lblSelectedAlbum);


		lblDescrizioneAlbum = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblDescrizioneAlbum.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblDescrizioneAlbum = new GridBagConstraints();
		gbc_lblDescrizioneAlbum.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblDescrizioneAlbum.insets = new Insets(0, 0, 5, 5);
		gbc_lblDescrizioneAlbum.gridx = 0;
		gbc_lblDescrizioneAlbum.gridy = 2;
		add(lblDescrizioneAlbum, gbc_lblDescrizioneAlbum);

		scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setOpaque(false);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 2;
		gbc_scrollPane.gridheight = 3;
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 2;
		add(scrollPane, gbc_scrollPane);

		txtrDescrizioneAlbum = new JTextArea();
		txtrDescrizioneAlbum.setDocument(new MaxLenghtTextDocument(800));
		txtrDescrizioneAlbum.setOpaque(false);
		scrollPane.setViewportView(txtrDescrizioneAlbum);
		txtrDescrizioneAlbum.setWrapStyleWord(true);
		txtrDescrizioneAlbum.setLineWrap(true);

		scrollPane_1 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_1.gridheight = 3;
		gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane_1.gridx = 5;
		gbc_scrollPane_1.gridy = 2;
		add(scrollPane_1, gbc_scrollPane_1);

		textAreaDescrizioneFoto = new JTextArea();
		textAreaDescrizioneFoto.setDocument(new MaxLenghtTextDocument(800));
		textAreaDescrizioneFoto.setOpaque(false);
		textAreaDescrizioneFoto.setWrapStyleWord(true);
		textAreaDescrizioneFoto.setLineWrap(true);
		scrollPane_1.setViewportView(textAreaDescrizioneFoto);

		btnCopertinaAlbum = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.btnCopertinaAlbum.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnCopertinaAlbum.addActionListener(albumCoverListener);

		lblAlbumReminder = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblAlbumReminder.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblAlbumReminder.setPreferredSize(new Dimension(120, 16));
		lblAlbumReminder.setFont(new Font("Dialog", Font.PLAIN, 9));
		GridBagConstraints gbc_lblAlbumReminder = new GridBagConstraints();
		gbc_lblAlbumReminder.anchor = GridBagConstraints.EAST;
		gbc_lblAlbumReminder.fill = GridBagConstraints.VERTICAL;
		gbc_lblAlbumReminder.gridheight = 2;
		gbc_lblAlbumReminder.insets = new Insets(0, 0, 5, 5);
		gbc_lblAlbumReminder.gridx = 0;
		gbc_lblAlbumReminder.gridy = 3;
		add(lblAlbumReminder, gbc_lblAlbumReminder);

		verticalBox = Box.createVerticalBox();
		GridBagConstraints gbc_verticalBox = new GridBagConstraints();
		gbc_verticalBox.gridheight = 2;
		gbc_verticalBox.insets = new Insets(0, 0, 5, 5);
		gbc_verticalBox.gridx = 4;
		gbc_verticalBox.gridy = 2;
		add(verticalBox, gbc_verticalBox);

		btnSelezionaUnaFoto = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.btnSelezionaUnaFoto.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnSelezionaUnaFoto.setAlignmentX(Component.CENTER_ALIGNMENT);
		verticalBox.add(btnSelezionaUnaFoto);

		verticalStrut = Box.createVerticalStrut(5);
		verticalBox.add(verticalStrut);

		btnSelezionaPiFoto = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.btnSelezionaPiFoto.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnSelezionaPiFoto.setAlignmentX(Component.CENTER_ALIGNMENT);
		verticalBox.add(btnSelezionaPiFoto);
		btnSelezionaPiFoto.addActionListener(selectMultiplePhotoListener);
		btnSelezionaUnaFoto.addActionListener(selectPhotoListener );

		btnModificaCopertina = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.btnModificaCopertina.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnModificaCopertina.addActionListener(modifyCoverListener);
		GridBagConstraints gbc_btnModificaCopertina = new GridBagConstraints();
		gbc_btnModificaCopertina.insets = new Insets(0, 0, 5, 5);
		gbc_btnModificaCopertina.gridx = 4;
		gbc_btnModificaCopertina.gridy = 4;
		add(btnModificaCopertina, gbc_btnModificaCopertina);
		GridBagConstraints gbc_btnCopertinaAlbum = new GridBagConstraints();
		gbc_btnCopertinaAlbum.insets = new Insets(0, 0, 0, 5);
		gbc_btnCopertinaAlbum.gridx = 0;
		gbc_btnCopertinaAlbum.gridy = 5;
		add(btnCopertinaAlbum, gbc_btnCopertinaAlbum);

		lblCopertina = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblCopertina.text")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_lblCopertina = new GridBagConstraints();
		gbc_lblCopertina.insets = new Insets(0, 0, 0, 5);
		gbc_lblCopertina.anchor = GridBagConstraints.WEST;
		gbc_lblCopertina.gridx = 1;
		gbc_lblCopertina.gridy = 5;
		add(lblCopertina, gbc_lblCopertina);

		btnAggiungiAlbum = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.btnAggiungiAlbum.text")); //$NON-NLS-1$ //$NON-NLS-2$
		btnAggiungiAlbum.addActionListener(addAlbumListener);

		GridBagConstraints gbc_btnAggiungiAlbum = new GridBagConstraints();
		gbc_btnAggiungiAlbum.insets = new Insets(0, 0, 0, 5);
		gbc_btnAggiungiAlbum.gridx = 2;
		gbc_btnAggiungiAlbum.gridy = 5;
		add(btnAggiungiAlbum, gbc_btnAggiungiAlbum);

		horizontalBox = Box.createHorizontalBox();
		GridBagConstraints gbc_horizontalBox = new GridBagConstraints();
		gbc_horizontalBox.insets = new Insets(0, 0, 0, 5);
		gbc_horizontalBox.gridx = 4;
		gbc_horizontalBox.gridy = 5;
		add(horizontalBox, gbc_horizontalBox);

		btnAggiungi = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.btnAggiungi.text")); //$NON-NLS-1$ //$NON-NLS-2$
		horizontalBox.add(btnAggiungi);

		horizontalStrut = Box.createHorizontalStrut(5);
		horizontalBox.add(horizontalStrut);

		btnReset = new JButton(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.btnReset.text")); //$NON-NLS-1$ //$NON-NLS-2$
		horizontalBox.add(btnReset);
		btnReset.addActionListener(resetAddPhotosWindowListener);
		btnAggiungi.addActionListener(addPhotosListener);

		lblPendingphotos = new JLabel(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblPendingphotos.text")); //$NON-NLS-1$ //$NON-NLS-2$
		lblPendingphotos.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblPendingphotos = new GridBagConstraints();
		gbc_lblPendingphotos.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblPendingphotos.gridx = 5;
		gbc_lblPendingphotos.gridy = 5;
		add(lblPendingphotos, gbc_lblPendingphotos);

	}

	public void setSelectedAlbum(PhotoAlbum album) {
		if (album.albumid == -1)
			return;
		if (!album.newAlbum) {
			currentlySelectedAlbum = album;
			lblSelectedAlbum.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.listCellRendererComponent.text2")+album.name);
		} else {
			currentlySelectedAlbum = null;
			lblSelectedAlbum.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.listCellRendererComponent.text3"));
		}
	}

	private void resetCreateAlbumWindow () {
		textField.setText("");
		txtrDescrizioneAlbum.setText("");
		lblCopertina.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblCopertina.text"));
		pendingAlbumCoverPath = null;
	}

	private void resetAddPhotosWindow () {
		textAreaDescrizioneFoto.setText("");
		lblPendingphotos.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.lblPendingphotos.text"));
		pendingPhotos = new ArrayList<Photo>();
	}

	private class AlbumBoxRenderer extends DefaultListCellRenderer {

		private DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {

			JLabel listCellRendererComponent = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
			if (value != null)
				listCellRendererComponent.setText(((PhotoAlbum)value).name);
			else
				listCellRendererComponent.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("AddPhotosPanel.listCellRendererComponent.text1"));

			return listCellRendererComponent;

		}
	}


}
